package com.nwpu.coursesupport.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.CourseSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.service.imple.CourseService;
import com.nwpu.coursesupport.service.param.CourseServiceParam;


@Controller
@RequestMapping("/course")
public class CourseController extends AbstractController {

	@Autowired
	private CourseService courseService;
	
    @RequestMapping(value="/detail/{id}",method=RequestMethod.GET)
    public String detail(@PathVariable("id")int id,HttpServletRequest request) {
        JSONResult rst= invokeService("course", "detail", "{\"id\":\"" + id + "\"}");
        request.setAttribute("data",parseData(rst));
        return "/teapage/courseDetail";
    }

    @RequestMapping(value="/ajaxDetail/{id}",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult ajaxDetail(@PathVariable("id") String id) {
        JSONResult result = invokeService("course", "detail", "{\"id\":\"" + id + "\"}");
        return result;
    }
    
    @RequestMapping("/list")
    public String list(Integer tid,Integer pageNo,HttpServletRequest request) {
    	/**
    	 * tid is null
    	 * 之前的tid全局变量已删除，获取id和roleType调用
    	 * getSession().getAttribute("id")和getSession().getAttribute("roleType")
    	 */
    	CourseServiceParam.SearchParam p=new CourseServiceParam.SearchParam();
    	if(pageNo==null){
    		pageNo=1;
    	}
    	p.setPageNo(pageNo);
    	if(tid!=null){
    		CourseSearchCriteria sc=new CourseSearchCriteria();
    		sc.setTid(tid);
    		p.setSearch(sc);
    	}
    	JSONResult rst=this.courseService.list(p);
        request.setAttribute("data", parseData(rst));
        return "/teapage/teacherIndex";
    }

    @RequestMapping(value="/listView",method=RequestMethod.GET)
    public String listView(Integer tid,HttpServletRequest request) {
    	
    	CourseSearchCriteria sc=null;
    	if(tid!=null){//如果
    		sc=new CourseSearchCriteria();
    		sc.setTid(tid); 
    	}
    	JSONResult rst=this.courseService.listView(sc);
        request.setAttribute("data", parseData(rst));
        return "/teapage/teacherIndex";
    }
    
    @RequestMapping(value="/ajaxListView",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult ajaxListView(@RequestParam("data")String data) {
    
    	JSONResult rst=invokeService("course", "listView", data);
        return rst;
    }
    

    @RequestMapping(value="/ajaxListData",method=RequestMethod.GET)
    @ResponseBody
    public JSONResult ajaxListData(@RequestParam("data")String data) {
    	JSONResult result = invokeService("course", "listData", data);
        return result;
    }

    @RequestMapping(value="/ajaxCreate",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult ajaxCreate(@RequestParam("data") String data) {
        JSONResult result = invokeService("course", "create", data);
        return result;
    }

    @RequestMapping(value="/ajaxUpdate",method=RequestMethod.POST)
    @ResponseBody
    public JSONResult ajaxUpdate(@RequestParam("data") String data) {
        JSONResult result = invokeService("course", "update", data);
        return result;
    }

 
}
